package com.ruoyi.syrw.common;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.math3.analysis.ParametricUnivariateFunction;

@Data
@NoArgsConstructor
public class JiPeiFunction implements ParametricUnivariateFunction {

    public JiPeiFunction(double b, double m) {
        this.b = Double.isNaN(b) ? 0 : b;
        this.m = Double.isNaN(m) ? 0 : m;
    }

    private double b;
    private double m;

    public JiPeiFunction(double[] best) {
        this.b = best[0];
        this.m = best[1];
    }

    public double getP(double d) {
        return this.value(d, b, m);
    }

    public double getD(double p) {
        return (60 * Math.pow(((p - p * b) / (100 - p * b)), (1 / m)));
    }


    @Override
    public double value(double d, double... parameters) {
        double b = parameters[0];
        double m = parameters[1];
        return (100 / ((1 - b) * Math.pow((60 / d), m) + b));
    }

    @Override
    public double[] gradient(double d, double... parameters) {
        double b = parameters[0];
        double m = parameters[1];
        double[] gradients = new double[2];
        double temp = (1 - b) * Math.pow((60 / d), m);
        gradients[0] = -Math.pow((100 * (1 - b) * m * Math.pow((60 / d), (m - 1)) * (-60) / Math.pow(d, 2)) /
            temp, 2);
        gradients[1] = -Math.pow((Math.pow(100 * (1 - b) * m * (60 / d), (m - 1)) * (-60) / Math.pow(d, 2)) /
            temp, 2);
        return gradients;
    }
}
